<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="是否字典类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['izDict', validatorRules.izDict]" @change="izDictChange" :trigger-change="true" dictCode="yn" placeholder="请选择是否是字典类型"/>
        </a-form-item>
        <a-form-item v-if="izDictBool" label="字典编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-search-select-tag :disabled="disableSubmit" v-decorator="['dictCode']" @change="dictCodeChange" dict="sys_dict,dict_name,dict_code" />
        </a-form-item>
        <a-form-item v-if="izDictBool" label="字典对应的值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['dictItemValue', validatorRules.dictItemValue]" :trigger-change="true" :dictCode="dictCodeText" placeholder="请选择字典对应的值"/>
        </a-form-item>
        <a-form-item v-if="!izDictBool" label="模版编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :disabled="disableSubmit" v-decorator="['code', validatorRules.code]" placeholder="请输入模版编码"></a-input>
        </a-form-item>
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="encoding_type" placeholder="请选择类型"/>
        </a-form-item>

        <a-form-item label="前缀1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-group v-decorator="['prefix1Type,prefix1,prefix1Length', validatorRules.length]"  :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
            <j-dict-select-tag @change="prefix1TypeChange" :disabled="disableSubmit" type="list" v-decorator="['prefix1Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="前缀1格式" style="width: 101px"/>
            <a-input @blur="setCoding" :disabled="dis.prefix1D || disableSubmit" v-decorator="['prefix1']" placeholder="请输入前缀1"  :style="{width: 'calc(100% - 201px)'}"></a-input>
            <a-input-number :disabled="dis.prefix1LengthD || disableSubmit" @change="prefix1LengthChange" v-decorator="['prefix1Length', validatorRules.length]" :max="20" :min="0" :precision="0" placeholder="长度" style="width: 100px"/>
          </a-input-group>
        </a-form-item>

        <a-form-item label="前缀2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-group v-decorator="['prefix2Type,prefix2,prefix2Length', validatorRules.length]" :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
            <j-dict-select-tag @change="prefix2TypeChange" :disabled="disableSubmit" type="list" v-decorator="['prefix2Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="前缀2格式" style="width: 101px"/>
            <a-input @blur="setCoding" :disabled="dis.prefix2D || disableSubmit" v-decorator="['prefix2']" placeholder="请输入前缀2"  :style="{width: 'calc(100% - 201px)'}"></a-input>
            <a-input-number :disabled="dis.prefix2LengthD || disableSubmit" @change="prefix2LengthChange" v-decorator="['prefix2Length', validatorRules.length]" :max="20" :min="0" :precision="0" placeholder="长度" style="width: 100px"/>
          </a-input-group>
        </a-form-item>

        <a-form-item label="前缀3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-group v-decorator="['prefix3Type,prefix3,prefix3Length', validatorRules.length]" :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
            <j-dict-select-tag @change="prefix3TypeChange" :disabled="disableSubmit" type="list" v-decorator="['prefix3Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="前缀3格式" style="width: 101px"/>
            <a-input @blur="setCoding" :disabled="dis.prefix3D || disableSubmit" v-decorator="['prefix3']" placeholder="请输入前缀3"  :style="{width: 'calc(100% - 201px)'}"></a-input>
            <a-input-number :disabled="dis.prefix3LengthD || disableSubmit" @change="prefix3LengthChange" v-decorator="['prefix3Length', validatorRules.length]" :max="20" :min="0" :precision="0" placeholder="长度" style="width: 100px"/>
          </a-input-group>
        </a-form-item>

        <a-form-item label="后缀1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-group v-decorator="['suffix1Type,suffix1,suffix1Length', validatorRules.length]" :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
            <j-dict-select-tag @change="suffix1TypeChange" :disabled="disableSubmit" type="list" v-decorator="['suffix1Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="后缀1格式" style="width: 101px"/>
            <a-input @blur="setCoding" :disabled="dis.suffix1D || disableSubmit" v-decorator="['suffix1']" placeholder="请输入后缀1"  :style="{width: 'calc(100% - 201px)'}"></a-input>
            <a-input-number :disabled="dis.suffix1LengthD || disableSubmit" @change="suffix1LengthChange" v-decorator="['suffix1Length', validatorRules.length]" :max="20" :min="0" :precision="0" placeholder="长度" style="width: 100px"/>
          </a-input-group>
        </a-form-item>

        <a-form-item label="后缀2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-group v-decorator="['suffix2Type,suffix2,suffix2Length', validatorRules.length]" :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
            <j-dict-select-tag @change="suffix2TypeChange" :disabled="disableSubmit" type="list" v-decorator="['suffix2Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="后缀2格式" style="width: 101px"/>
            <a-input @blur="setCoding" :disabled="dis.suffix2D || disableSubmit" v-decorator="['suffix2']" placeholder="请输入后缀2"  :style="{width: 'calc(100% - 201px)'}"></a-input>
            <a-input-number :disabled="dis.suffix2LengthD || disableSubmit" @change="suffix2LengthChange" v-decorator="['suffix2Length', validatorRules.length]" :max="20" :min="0" :precision="0" placeholder="长度" style="width: 100px"/>
          </a-input-group>
        </a-form-item>

        <a-form-item label="后缀3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-group v-decorator="['suffix3Type,suffix3,suffix3Length', validatorRules.length]" :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
            <j-dict-select-tag @change="suffix3TypeChange" :disabled="disableSubmit" type="list" v-decorator="['suffix3Type']" :trigger-change="true" dictCode="encoding_rules_type" placeholder="后缀3格式" style="width: 101px"/>
            <a-input @blur="setCoding" :disabled="dis.suffix3D || disableSubmit" v-decorator="['suffix3']" placeholder="请输入后缀3"  :style="{width: 'calc(100% - 201px)'}"></a-input>
            <a-input-number :disabled="dis.suffix3LengthD || disableSubmit" @change="suffix3LengthChange" v-decorator="['suffix3Length', validatorRules.length]" :max="20" :min="0" :precision="0" placeholder="长度" style="width: 100px"/>
          </a-input-group>
        </a-form-item>


        <a-form-item v-is-group label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-company-select-tag placeholder="请选择所属公司" v-decorator="['companyId', validatorRules.companyId]"
                                :trigger-change="true" :disabled="disableSubmit" />
        </a-form-item>

        <a-form-item label="编码预览" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['coding']" :disabled="true"></a-input>
        </a-form-item>

        <a-form-item label="当前值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['codingNow']" :disabled="true"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import { IS_GROUP } from '@/store/mutation-types'
  import JCompanySelectTag from '@comp/jeecg/JCompanySelectTag'


  export default {
    name: "EncodingRulesModal",
    components: {
      JDictSelectTag,
      JCompanySelectTag,
      JSearchSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        disableSubmit: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          izDict: {
            initialValue: 0,
            rules: [
              { required: true, message: '请选择是否是字典类型!' },
            ]
          },
          type: {
            initialValue: 1,
            rules: [
              { required: true, message: '请选择类型!' },
            ]
          },
          code: {
            rules: [
              { required: this.izDictBool, message: '请输入规则编码!' },
            ]
          },
          dictItemValue: {
            rules: [
              { required: this.izDictBool, message: '请选择字典的值!' },
            ]
          },
          length: {
            rules: [
              // { required: false, message: '请输入提前缴费天数!' },
              // { pattern: /^([1-9]|1[0-9]|20|0)$/, message: '请输入0-20的整数' }
              // { pattern: /\{([1-9]|1[0-9]|20|0)\}/g, message: '请输入0-20的整数' }
            ],
          },
          suffix3Length: {
            rules: [
              // { required: false, message: '请输入提前缴费天数!' },
              { pattern: /^([1-9]\d*|[0]{1,1})$/, message: '请输入零或零以上的整数' }
            ],
          },
        },
        url: {
          add: "/system/encodingRules/add",
          edit: "/system/encodingRules/edit",
        },

        izDictBool: false,
        dictCodeText: "",

        dis:{
          prefix1D:false,
          prefix1LengthD:false,

          prefix2D:false,
          prefix2LengthD:false,

          prefix3D:false,
          prefix3LengthD:false,

          suffix1D:false,
          suffix1LengthD:false,

          suffix2D:false,
          suffix2LengthD:false,

          suffix3D:false,
          suffix3LengthD:false,
        },
        date:{},
      }
    },
    created () {
      let nowDate = new Date()
      let date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        day: nowDate.getDate()
      }
      this.date = date
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.dictCodeChange(this.model.dictCode)
        this.visible = true;
        this.$nextTick(() => {
          let keys = Object.keys(this.form.formItems);
          this.form.setFieldsValue(pick(this.model, keys))
          // this.form.setFieldsValue(pick(this.model,'code','izDict','dictCode','dictItemValue','type','prefix1','prefix1Type','prefix1Length','prefix2','prefix2Type','prefix2Length','prefix3','prefix3Type','prefix3Length','suffix1','suffix1Type','suffix1Length','suffix2','suffix2Type','suffix2Length','suffix3','suffix3Type','suffix3Length','coding','codingNow'))
          // this.typeChange(this.model.prefix1Type, this.model.prefix1Length, 'prefix1')
          // this.typeChange(this.model.prefix2Type, this.model.prefix2Length, 'prefix2')
          // this.typeChange(this.model.prefix3Type, this.model.prefix3Length, 'prefix3')
          // this.typeChange(this.model.suffix1Type, this.model.suffix1Length, 'suffix1')
          // this.typeChange(this.model.suffix2Type, this.model.suffix2Length, 'suffix2')
          // this.typeChange(this.model.suffix3Type, this.model.suffix3Length, 'suffix3')
          this.setDi(this.model.prefix1Type, 'prefix1')
          this.setDi(this.model.prefix2Type, 'prefix2')
          this.setDi(this.model.prefix3Type, 'prefix3')
          this.setDi(this.model.suffix1Type, 'suffix1')
          this.setDi(this.model.suffix2Type, 'suffix2')
          this.setDi(this.model.suffix3Type, 'suffix3')
        })
        this.izDictChange(this.model.izDict)

        let isGroup = sessionStorage.getItem(IS_GROUP)
        if (isGroup == 1) {
          let companyName = {
            rules: [{
              required: true, message: '请选择所属公司!'
            }]
          }
          this.validatorRules.companyId = companyName
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      izDictChange (val) {
        if (val == 1){
          this.izDictBool = true
          this.validatorRules.code.rules[0].required = false
          this.validatorRules.dictItemValue.rules[0].required = true
        } else {
          this.izDictBool = false
          this.validatorRules.code.rules[0].required = true
          this.validatorRules.dictItemValue.rules[0].required = false
        }
      },
      prefix1TypeChange (val) {
        let length = this.form.getFieldValue("prefix1Length")
        this.typeChange(val, length, 'prefix1')
      },
      prefix1LengthChange (val) {
        let type = this.form.getFieldValue("prefix1Type")
        this.typeChange(type, val, 'prefix1')
      },
      prefix2TypeChange (val) {
        let length = this.form.getFieldValue("prefix2Length")
        this.typeChange(val, length, 'prefix2')
      },
      prefix2LengthChange (val) {
        let type = this.form.getFieldValue("prefix2Type")
        this.typeChange(type, val, 'prefix2')
      },
      prefix3TypeChange (val) {
        let length = this.form.getFieldValue("prefix3Length")
        this.typeChange(val, length, 'prefix3')
      },
      prefix3LengthChange (val) {
        let type = this.form.getFieldValue("prefix3Type")
        this.typeChange(type, val, 'prefix3')
      },

      suffix1TypeChange (val) {
        let length = this.form.getFieldValue("suffix1Length")
        this.typeChange(val, length, 'suffix1')
      },
      suffix1LengthChange (val) {
        let type = this.form.getFieldValue("suffix1Type")
        this.typeChange(type, val, 'suffix1')
      },
      suffix2TypeChange (val) {
        let length = this.form.getFieldValue("suffix2Length")
        this.typeChange(val, length, 'suffix2')
      },
      suffix2LengthChange (val) {
        let type = this.form.getFieldValue("suffix2Type")
        this.typeChange(type, val, 'suffix2')
      },
      suffix3TypeChange (val) {
        let length = this.form.getFieldValue("suffix3Length")
        this.typeChange(val, length, 'suffix3')
      },
      suffix3LengthChange (val) {
        let type = this.form.getFieldValue("suffix3Type")
        this.typeChange(type, val, 'suffix3')
      },

      typeChange (type, length, t){
        let tt = '', th = t+"Length"

        if (length && length > 20){
          length = 20
        }
        if (type == 0){
          length = 0
        } else if (type == 1){
          tt = this.date.year + '' + this.date.month + '' + this.date.day
          length = 0
        } else if (type == 2){
          tt = this.date.year + '' + this.date.month
          length = 0
        } else if (type == 3){
          tt = this.date.year
          length = 0
        } else if (type == 4 || type == 5){
          let ta = ''
          if (type == 4){
            ta = 'a'
          } else if (type == 5){
            ta = 'A'
          }
          tt = ta
          if (length && length > 1){
            for (let i = 1; i < length; i++) {
              tt = ta + '' + tt
            }
          } else {
            length = 1
          }
        } else if (type == 6){
          tt = '1'
          if (length && length > 1){
            for (let i = 1; i < length; i++) {
              tt = '0' + tt
            }
          } else {
            length = 1
          }
        } else {
          length = 0
        }
        let m = {}
        m[t] = tt
        m[th] = length
        this.$nextTick(() => {
          this.form.setFieldsValue(m)
          this.setCoding()
        })
        this.setDi(type, t)
      },
      setDi(type, t){
        let td = false, thd = false
        if (type == 0){
          td = false
          thd = true
        } else if (type == 1 || type == 2 || type == 3){
          td = true
          thd = true
        }else if (type == 4 || type == 5 || type == 6){
          td = true
          thd = false
        } else {
          td = true
          thd = true
        }
        this.dis[t+"D"] = td
        this.dis[ t+"LengthD"] = thd
      },
      setCoding(){
        let prefix1 = this.form.getFieldValue("prefix1")
        if (!prefix1){prefix1 = ""}
        let prefix2 = this.form.getFieldValue("prefix2")
        if (!prefix2){prefix2 = ""}
        let prefix3 = this.form.getFieldValue("prefix3")
        if (!prefix3){prefix3 = ""}
        let suffix1 = this.form.getFieldValue("suffix1")
        if (!suffix1){suffix1 = ""}
        let suffix2 = this.form.getFieldValue("suffix2")
        if (!suffix2){suffix2 = ""}
        let suffix3 = this.form.getFieldValue("suffix3")
        if (!suffix3){suffix3 = ""}
        let coding = prefix1 + prefix2 + prefix3 + suffix1 + suffix2 + suffix3
        this.$nextTick(() => {
          this.form.setFieldsValue({'coding': coding})
        })
      },
      dictCodeChange (val) {
        this.dictCodeText = val
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        let keys = Object.keys(this.form.formItems);
        this.form.setFieldsValue(pick(row, keys))
        // this.form.setFieldsValue(pick(row,'code','izDict','dictCode','dictItemValue','type','prefix1','prefix1Type','prefix1Length','prefix2','prefix2Type','prefix2Length','prefix3','prefix3Type','prefix3Length','suffix1','suffix1Type','suffix1Length','suffix2','suffix2Type','suffix2Length','suffix3','suffix3Type','suffix3Length','coding','codingNow'))
      },


    }
  }
</script>